import urllib
import urllib2, time
import md5

host = "192.168.25.1:8888"
url = "http://" + host + "/api/trigger"

#apikey = "PCRwHtiC8NzmQFnKsrGqkVmgTI1uayBL"
#secure_key = "PCRwHtiC8NzmQFnKsrGqkVmgTI1uayBL"
apikey = "PCRwHtiC8NzmQFnKsrGqkVmgTI1uayBL"
secure_key = "PCRwHtiC8NzmQFnKsrGqkVmgTI1uayBL"
#params = {"tkey":"ssss", "data":"just a test"}
params = {"tkey":"fsdf", "data":"trigger test"}

def trigger(params, apikey, secure_key):
    global url

    params["t"] = str(int(time.time()))
    params['k'] = apikey

    s = md5_sign(params, secure_key)
    params['s'] = s

    url = url + "?%s" % urllib.urlencode(params)
    print url
    rep = urllib2.urlopen(url)
    print rep.read()


def md5_sign(params, secure_key):
    str_for_sign = ""
    k_list = []
    for k in params:
        k_list.append(k)
    k_list.sort()

    for k in k_list:
        str_for_sign = str_for_sign + k + params[k]

    str_for_sign = str_for_sign + secure_key
    print str_for_sign
    m = md5.new()
    m.update(str_for_sign)
    return m.hexdigest()


if __name__ == '__main__':
    trigger(params, apikey, secure_key)

